#include "remote.h"


#ifdef  REMOTE_EN
#include "gpio.h"
#include "log.h"
#include "adc_voice.h"
#include "clock.h"
#include "timer.h"
#include "led_di.h"
#include "OSAL.h"
#include "peripheral.h"
#include "simpleBLEPeripheral.h"

#define REMOTE_PIN  P31

//红外遥控器................................................................../
#if 1
//使用数值加减来实现时间延迟（200us）
//4.5ms     
#define _start_high_us_min  15  //同步信号的高电平持续时间，最低值3ms
#define _start_high_us_max  30 //同步信号的高电平持续时间，最高值6ms 
//560us
#define _num0_us_min        2   //数据0，高电平信号持续时间，最低值400us
#define _num0_us_max        4   //数据0，高电平信号持续时间，最高值700us
//1690
#define _num1_us_min        7  //数据1，高电平信号持续时间，最低值1500us
#define _num1_us_max        10  //数据1，高电平信号持续时间，最高值1900us

#else
//使用数值加减来实现时间延迟（50us）
//4.5ms     
#define _start_high_us_min  60  //同步信号的高电平持续时间，最低值3ms
#define _start_high_us_max  120 //同步信号的高电平持续时间，最高值6ms 
//560us
#define _num0_us_min        8   //数据0，高电平信号持续时间，最低值400us
#define _num0_us_max        14  //数据0，高电平信号持续时间，最高值700us
//1690
#define _num1_us_min        30  //数据1，高电平信号持续时间，最低值1500us
#define _num1_us_max        38  //数据1，高电平信号持续时间，最高值1900us

#endif


uint32_t Jump_flag;
uint32_t High;              //高电平计时     
uint32_t start_high_flag;
uint32_t cntint;            //接收数据的长度
uint32_t RF_data;           //临时存放接收的数据        
uint32_t dataJ;             //接受的最终数据
uint8_t remote_flag = 0;
uint8_t remote_flag1 = 0;
uint8 initial_advertising_enable1 = true;//false
			
void Remote_decode(void);
void Remote_count(void);
void remote_to_agreement(void);
void timer_int_process(uint8_t evt)
{
    
    switch(evt)
    {
    case HAL_EVT_TIMER_5:
        
        Remote_count();				
//			High++; //电平持续时间累积
        break;
    case  AP_TIMER_ID_6:
        if(flashStruct.modeIndex_falsh < 22 && flashStruct.modeIndex_falsh > 17 && !remote_flag)
        {
            adcMeasureTask();
        }     
        break;
    default:
        LOG("err ");
        break;
    }
}

//void ir_irq_handler(GPIO_Pin_e pin,IO_Wakeup_Pol_e type)
//{
//	if(pin == REMOTE_PIN  && type == POL_RISING)
//	{		
//		if(initial_advertising_enable1 == true)
//		{
//			// 3. disconnect all connection
//			GAPRole_TerminateConnection();//断开连接
//			initial_advertising_enable1 = false;
//			GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &initial_advertising_enable1 );
//		}
//		if(flashStruct.modeIndex_falsh < 22 && flashStruct.modeIndex_falsh > 17 && !remote_flag)
//			hal_timer_stop(AP_TIMER_ID_6);
//		hal_timer_set(AP_TIMER_ID_5,50);


//	}
//	else if(pin == REMOTE_PIN  && type == POL_FALLING)
//	{
//		hal_timer_stop(AP_TIMER_ID_5);
//		Jump_flag = 1;  //上升沿
//		Remote_decode();
//		High = 0;
//	}
//}


void remote_init(void)
{
    hal_gpio_init();
    hal_gpio_pin_init(REMOTE_PIN,IE);
    hal_gpio_pull_set(REMOTE_PIN,GPIO_PULL_DOWN);
//		hal_gpioin_register(REMOTE_PIN,ir_irq_handler,ir_irq_handler);
    // hal_timer_init(timer_int_process);
	hal_timer_set(AP_TIMER_ID_5,200);
}


void Remote_count(void)
{
    //if(hal_gpio_read(REMOTE_PIN))//时间太久，导致控制不实时
    if(((AP_GPIO->ext_porta) >> REMOTE_PIN )& 1) //高电平
    {

        High++; //电平持续时间累积
        if(Jump_flag == 1){Jump_flag = 0;}//防误触发，需确认为下降沿
    }
    else //低电平
    {
        if(Jump_flag == 0)  //防误触发，确认为上升沿，则低电平累积时间结束
        {
            Jump_flag = 1;  //上升沿
            Remote_decode();//对High的计数长度进行处理
            High = 0;
        }
    }
}


void Remote_decode(void)//接收码处理函数
{
    if((High >= _start_high_us_min) && (High <= _start_high_us_max))
    {
        remote_flag = 1;
							
        // 3. disconnect all connection
        GAPRole_TerminateConnection();//断开连接
        if(initial_advertising_enable1 == true)
        {

            initial_advertising_enable1 = false;
            GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &initial_advertising_enable1 );
        }
        // __disable_irq();
        start_high_flag = 1;//同步结束，开始接收数据
        cntint = 0;         //数据长度
        RF_data = 0;        //数据临时存放地址
    }
    if(cntint < 32 && start_high_flag == 1)//接收数据中,这里只检测高电平的持续时间，因为低电平0码和1码的持续时间都一样
    {
        if((High >= _num0_us_min) && (High <= _num0_us_max))        //数据0
        {
            RF_data = RF_data<<1;
            cntint++;
        }
        else if((High >= _num1_us_min) && (High <= _num1_us_max))   //数据1
        {
            RF_data = RF_data<<1;
            RF_data |= 1;
            cntint++;
        }
        else
        {
            //数据错误，清零重新等待
            cntint = 0;
        }

    }

    if(cntint == 32)    //长度抵达32bit，开始处理
    {
        remote_flag1 = 1;
        dataJ = RF_data;    //记下当前数据
        RF_data = 0;  
        cntint=0;
        start_high_flag=0;

			
				
				initial_advertising_enable1 = true;//false
				osal_stop_timerEx(simpleBLEPeripheral_TaskID, SBP_RESET_ADV_EVT);
				osal_start_timerEx(simpleBLEPeripheral_TaskID, SBP_RESET_ADV_EVT,1000);
        // __enable_irq();  
    }
}


void remote_to_agreement(void)
{
    
    if(dataJ != 0 && (dataJ >> 16 & 0xFFFF) == 0x47df)
    {

        my_strcut remote_to_Task = {0};
        remote_to_Task.data[0] = 0xbc;
       switch(dataJ & 0xFFFF)
       {
        case 0xa45b://关灯
            remote_to_Task.data[1] = 0x01;
            remote_to_Task.data[2] = 0x01;
            remote_to_Task.data[3] = 0x00;
            break;
        case 0xb44b://自动
            remote_to_Task.data[1] = 0x16;
            remote_to_Task.data[2] = 0x01;
            remote_to_Task.data[3] = 0x01;            
            break;
        case 0x649b://开灯
            remote_to_Task.data[1] = 0x01;
            remote_to_Task.data[2] = 0x01;
            remote_to_Task.data[3] = 0x01;
            break;
        case 0xd02f://红色
            remote_to_Task.data[1] = 0x04;
            remote_to_Task.data[2] = 0x06;
            remote_to_Task.data[3] = 0x00;
            remote_to_Task.data[4] = 0x00;
            remote_to_Task.data[5] = 0x03;
            remote_to_Task.data[6] = 0xE8;
            break;
        case 0x50af://绿色
            remote_to_Task.data[1] = 0x04;
            remote_to_Task.data[2] = 0x06;
            remote_to_Task.data[3] = 0x00;
            remote_to_Task.data[4] = 0x78;
            remote_to_Task.data[5] = 0x03;
            remote_to_Task.data[6] = 0xE8;
            break;
        case 0xf00f://蓝色
            remote_to_Task.data[1] = 0x04;
            remote_to_Task.data[2] = 0x06;
            remote_to_Task.data[3] = 0x00;
            remote_to_Task.data[4] = 0xF0;
            remote_to_Task.data[5] = 0x03;
            remote_to_Task.data[6] = 0xE8;
            break;
        case 0xa250://黄色
            remote_to_Task.data[1] = 0x04;
            remote_to_Task.data[2] = 0x06;
            remote_to_Task.data[3] = 0x00;
            remote_to_Task.data[4] = 0x22;
            remote_to_Task.data[5] = 0x03;
            remote_to_Task.data[6] = 0x84;
            break;
        case 0x3ac5://浅绿
            remote_to_Task.data[1] = 0x04;
            remote_to_Task.data[2] = 0x06;
            remote_to_Task.data[3] = 0x00;
            remote_to_Task.data[4] = 0x78;
            remote_to_Task.data[5] = 0x03;
            remote_to_Task.data[6] = 0x20;
            break;
        case 0x7689://浅蓝
            remote_to_Task.data[1] = 0x04;
            remote_to_Task.data[2] = 0x06;
            remote_to_Task.data[3] = 0x00;
            remote_to_Task.data[4] = 0xF0;
            remote_to_Task.data[5] = 0x03;
            remote_to_Task.data[6] = 0x20;
            break;
        case 0x609f://红色
            remote_to_Task.data[1] = 0x04;
            remote_to_Task.data[2] = 0x06;
            remote_to_Task.data[3] = 0x00;
            remote_to_Task.data[4] = 0x00;
            remote_to_Task.data[5] = 0x03;
            remote_to_Task.data[6] = 0xE8;
            break;
        case 0x10ef://紫色
            remote_to_Task.data[1] = 0x04;
            remote_to_Task.data[2] = 0x06;
            remote_to_Task.data[3] = 0x02;
            remote_to_Task.data[4] = 0x83;
            remote_to_Task.data[5] = 0x01;
            remote_to_Task.data[6] = 0x88;
            break;
        case 0x5aa5://双w
            remote_to_Task.data[1] = 0x13;
            remote_to_Task.data[2] = 0x01;
            remote_to_Task.data[3] = 0x01;
            break;
        case 0x906f://灯mode速度++
            remote_to_Task.data[1] = 0x08;
            remote_to_Task.data[2] = 0x01;
            if(gobalVar.flowSpeed + 20 > 100)gobalVar.flowSpeed = 100;
            else gobalVar.flowSpeed += 20;
            remote_to_Task.data[3] = gobalVar.flowSpeed;
            break;
        case 0x51ae://mode++
            remote_to_Task.data[1] = 0x06;
            remote_to_Task.data[2] = 0x02;
            remote_to_Task.data[3] = 0x00;  
           
            if(msg_2.led_mode > 17 && msg_2.led_mode < 22)msg_2.led_mode = 1;
            else if(msg_2.led_mode != 17 && msg_2.led_mode < 25)msg_2.led_mode +=1;
            else if(msg_2.led_mode >= 25)msg_2.led_mode = 1;
            else msg_2.led_mode = 22;
            remote_to_Task.data[4] = msg_2.led_mode;       
            break;
        case 0x08f7://灯亮度++
            remote_to_Task.data[1] = 0x13;
            remote_to_Task.data[2] = 0x03;
            ww_gobalVar.ww_brihgtness_a = ww_gobalVar.ww_brihgtness_a * 100 + 20;
            if(ww_gobalVar.ww_brihgtness_a > 100)ww_gobalVar.ww_brihgtness_a = 100;
            else ww_gobalVar.ww_brihgtness_a += 20;
            remote_to_Task.data[3] = (uint8_t)ww_gobalVar.ww_brihgtness_a;    
  
            break;
        case 0x38c7://灯mode速度--
            remote_to_Task.data[1] = 0x08;
            remote_to_Task.data[2] = 0x01;
            if(gobalVar.flowSpeed < 20)gobalVar.flowSpeed = 0;
            else gobalVar.flowSpeed -= 20;
            remote_to_Task.data[3] = gobalVar.flowSpeed;           
            break;
        case 0x4ab5://mode--
            remote_to_Task.data[1] = 0x06;
            remote_to_Task.data[2] = 0x02;
            remote_to_Task.data[3] = 0x00;  
            if(msg_2.led_mode > 17 && msg_2.led_mode < 22)msg_2.led_mode = 1;
            else if(msg_2.led_mode == 1 || msg_2.led_mode == 0)msg_2.led_mode = 25;
            else if(msg_2.led_mode != 22)msg_2.led_mode -=1;
            else msg_2.led_mode = 17;
            remote_to_Task.data[4] = msg_2.led_mode;       
            break;
        case 0xca35://灯亮度--
            remote_to_Task.data[1] = 0x13;
            remote_to_Task.data[2] = 0x03;
            ww_gobalVar.ww_brihgtness_a = ww_gobalVar.ww_brihgtness_a * 100 - 20;
            if(ww_gobalVar.ww_brihgtness_a < 0)ww_gobalVar.ww_brihgtness_a = 0;
            else ww_gobalVar.ww_brihgtness_a -= 20;
            remote_to_Task.data[3] = (uint8_t)ww_gobalVar.ww_brihgtness_a;    
            hal_timer_set(AP_TIMER_ID_6,20000);
            break;
        case 0x30cf://音乐模式1
            remote_to_Task.data[1] = 0x06;
            remote_to_Task.data[2] = 0x02;
            remote_to_Task.data[3] = 0x00;  
            remote_to_Task.data[4] = 19;   
            hal_timer_set(AP_TIMER_ID_6,20000);       
            break;
        case 0x58a7://音乐模式2
            remote_to_Task.data[1] = 0x06;
            remote_to_Task.data[2] = 0x02;
            remote_to_Task.data[3] = 0x00;  
            remote_to_Task.data[4] = 20;
            hal_timer_set(AP_TIMER_ID_6,20000);
            break;
        case 0xf807://音乐模式3
            remote_to_Task.data[1] = 0x06;
            remote_to_Task.data[2] = 0x02;
            remote_to_Task.data[3] = 0x00;  
            remote_to_Task.data[4] = 21;
            hal_timer_set(AP_TIMER_ID_6,20000);
            break;
        case 0x807f:
        //定时1H
            remote_to_Task.data[1] = 0x0d;
            remote_to_Task.data[2] = 0x06;
            remote_to_Task.data[3] = 0x00;  
            remote_to_Task.data[4] = 0x36;
            remote_to_Task.data[5] = 0xEE;  
            remote_to_Task.data[6] = 0x80;
            break;
        case 0x00ff:
            //定时2H
            remote_to_Task.data[1] = 0x0d;
            remote_to_Task.data[2] = 0x06;
            remote_to_Task.data[3] = 0x00;  
            remote_to_Task.data[4] = 0x6D;
            remote_to_Task.data[5] = 0xDD;  
            remote_to_Task.data[6] = 0x00;
            break;
        case 0x40bf:
            //定时4H
            remote_to_Task.data[1] = 0x0d;
            remote_to_Task.data[2] = 0x06;
            remote_to_Task.data[3] = 0x00;  
            remote_to_Task.data[4] = 0xDB;
            remote_to_Task.data[5] = 0xBA;  
            remote_to_Task.data[6] = 0x00;
            break;

       }
        MyTask_SendData(LED_TaskID,&remote_to_Task);
        osal_set_event(LED_TaskID,LED_Task_EVT);
        dataJ = 0;
    }
}


//红外遥控器................................................................../



//2.4g遥控器................................................................../




//2.4g遥控器................................................................../
#endif



